package com.axonactive.yolo.validation;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

import com.axonactive.yolo.dto.Parameters;
import com.axonactive.yolo.util.CONSTANTS;

public class ParamValidator {
	private static final Logger LOGGER = Logger.getLogger(TaskValidator.class);
	private static final SimpleDateFormat SDF = new SimpleDateFormat(
			"yyyy/MM/dd");

	public String validate(Parameters parameters) {

		// valid from & to Location
		if ("".equals(parameters.getFromLocation())
				|| "".equals(parameters.getToLocation())) {
			LOGGER.info("from to null");
			return "inValidParam";
		} else if (!"".equals(parameters.getFromLocation())
				&& !"".equals(parameters.getToLocation())
				&& parameters
						.getFromLocation()
						.substring(0, 3)
						.equalsIgnoreCase(
								parameters.getToLocation().substring(0, 3))) {
			return "inValidFromTo";
		}
		// valid for return date < depart date
		if (CONSTANTS.ROUND_TRIP.equals(parameters.getTypeOfTrip())) {
			if ("".equals(parameters.getInBound())) {
				LOGGER.info("inbound null");
				return "inValidParam";
			} else {
				String outbound = parameters.getOutBound();
				String inbound = parameters.getInBound();
				Date outboundDate = null;
				Date inboundDate = null;
				try {
					outboundDate = SDF.parse(outbound);
					inboundDate = SDF.parse(inbound);
					if (inboundDate.compareTo(outboundDate) < 0) {
						// error
						LOGGER.info("round trip null");
						return "inValidParam";
					}
				} catch (ParseException e) {
					LOGGER.error("ParseException: ", e);
				}

			}
		}
		return "nonerror";
	}

}
